package com.hgc.ch01.sync;

/**
 * Created by 01367627 on 2019/6/14.
 */
public class SyncObject {

    private static class Art13{

    }

    private static Art13 art13 = new Art13();

    private static class James extends Thread{

        public James(){
            setName("James");
        }

        @Override
        public void run() {
            //syncMethod();
            synchronized (Art13.class){
                System.out.println(getName() + " Got Art13 Class!");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(getName() + " Run Over");
            }
        }
    }

    private static class Lison extends Thread{

        public Lison(){
            setName("Lison");
        }

        @Override
        public void run() {
            //notSyncMethod();
            synchronized (art13){
                System.out.println(getName() + " Got Art13 Instance!");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(getName() + " Run Over");
            }
        }
    }

    synchronized static void syncMethod(){
        System.out.println(Thread.currentThread().getName() + " Running syncMethod.");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    static void notSyncMethod(){
        System.out.println(Thread.currentThread().getName() + " Running notSyncMethod.");
    }

    public static void main(String[] args) {
        Thread t1 = new James();
        Thread t2 = new Lison();
        t1.start();
        t2.start();
    }

}
